package org.itboys.mobile.service.mysql.web.wallet;

import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.random.RandomUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.dao.wallet.SundayWalletConchMapper;
import org.itboys.mobile.entity.mysql.wallet.SundayWallet;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletConch;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2017/12/19.
 * 圣代_钱包_service
 */
/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_钱包_service_web
 *
 */
@Service
public class SundayWalletService extends BaseMySqlService<SundayWallet>{
     @Autowired
    private SundayWalletConchMapper walletCodeMapper;

    /**
     * 获取用户钱包
     * @param memberId
     * @return
     */
    @Transactional
    public SundayWallet getByMemberId(Long memberId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("memberId",memberId);
        List<SundayWallet> walletList = super.select(param);
        SundayWallet wallet = null;
        if(ListUtil.isNotNull(walletList)){
            wallet = walletList.get(0);
        }else{
            wallet = new SundayWallet(memberId);
            super.insert(wallet);
            wallet = super.find(wallet.getId());
        }
        return wallet;
    }

    /**
     * 获取充值码
     * @param request
     * @param status
     * @return
     */
    public PageResult<SundayWalletConch> selectCode(HttpServletRequest request, Integer status){
        Long loginUserId= AdminSessionHolder.getAdminUserId();
        if(!LongUtil.isNotZreo(loginUserId))return new PageResult<>();
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"mobile","openId");
        if(status!=null){
            param.put("status",status);
        }
        Serializable count = walletCodeMapper.count(param);
        return  new PageResult<SundayWalletConch>(walletCodeMapper.select(param),count==null?0l:Long.valueOf(count.toString()));
    }
    /**
     *
     */
    /**
     * 批量充值码
     * @param num
     */
    public void generateCode(Integer num, BigDecimal amount){
        Long loginUserId= AdminSessionHolder.getAdminUserId();
        if(!LongUtil.isNotZreo(loginUserId))return;
        for(int i=0;i<num;i++){
            SundayWalletConch walletConch = new SundayWalletConch(RandomUtils.getClearRandomStr(32),amount);
            walletCodeMapper.insert(walletConch);
        }
    }
}
